<template>
  <el-dialog :title="bigTitle" :visible="showDialog" @close="close">
    <!-- 表单内容 -->
    <el-form ref="addDept" :model="deptForm" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="deptForm.name" placeholder="2-10字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="deptForm.code" placeholder="2-10字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <el-select
          v-model="deptForm.managerId"
          placeholder="请选择部门负责人"
          style="width: 80%"
          size="mini"
        >
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>

      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input
          v-model="deptForm.introduce"
          placeholder="1-100字符"
          style="width: 80%"
          type="textarea"
          :rows="4"
          size="mini"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="btnOK">确认</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartment, getDepartmentList, getMangerLists, getDepartmentDetail, updateDepartmentDetail } from '@/api/departments'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      deptForm: {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '',
        introduce: '',
        pid: ''// 父级部门id
      },
      // 负责人列表 接收
      managerList: [],
      rules: {
        // 定义规则
        name: [{ required: true, message: '输入不为空', trigger: 'blur' }, { min: 2, max: 10, message: '2-10字符', trigger: 'blur' }, {
          validator: async(rule, value, callback) => {
            let res = await getDepartmentList()
            if (this.deptForm.id) {
              res = res.filter(item => item.id !== this.currentNodeId)
            }
            if (res.some(item => item.name === value)) {
              callback(new Error('已存在'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        code: [{ required: true, message: '输入不为空', trigger: 'blur' }, { min: 2, max: 10, message: '2-10字符', trigger: 'blur' }, {
          validator: async(rule, value, callback) => {
            let res = await getDepartmentList()
            if (this.deptForm.id) {
              res = res.filter(item => item.id !== this.currentNodeId)
            }
            if (res.some(item => item.code === value)) {
              callback(new Error('已存在'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }],
        managerId: [{ required: true, message: '输入不为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '输入不为空', trigger: 'blur' }, { min: 2, max: 100, message: '2-10字符', trigger: 'blur' }, {
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {
    bigTitle() {
      return this.deptForm.id ? '修改部门' : '新增部门'
    }
  },
  created() {
    this.getMangerList()
  },
  methods: {
    // 通过id获取部门数据 将来回显到表单中
    async getDepartmentDetail() {
      console.log('子级获取的父级id', this.currentNodeId)
      // 调用获取部门数据接口的方法
      const res = await getDepartmentDetail(this.currentNodeId)
      console.log(res)
      // 拿到数据 给表单赋值  回显数据
      this.deptForm = res
    },
    // 获取负责人列表
    async getMangerList() {
      // 调用获取负责人接口列表的方法
      this.managerList = await getMangerLists()
      console.log(this.managerList)
    },
    btnOK() {
      // 对表单做整体校验
      // 校验通过 调用新增子部门接口方法
      // 成功 友好提示用户
      // 充值表单 关闭弹窗
      // 再次刷新组织架构页面
      this.$refs.addDept.validate(async isOK => {
        if (isOK) {
          if (this.deptForm.id) {
            // 区分新增和编辑
            await updateDepartmentDetail(this.deptForm)
            this.$message.success('修改成功啦')
            this.$emit('updateDepartment')
          } else {
            // 先把对象展开 再将后边的pid设置的结果覆盖前边的pid
            await addDepartment({ ...this.deptForm, pid: this.currentNodeId })
          }
          // this.deptForm.pid 等价于 { ...this.deptForm, pid: this.currentNodeId }
          // so 以上代码等价于
          // this.deptForm.pid = this.currentNodeId
          // await addDepartment（this.deptForm）
          this.$message.success('添加成功啦')
          this.$emit('updateDepartment')
          this.close()
        }
      })
    },
    close() {
      this.$refs.addDept.resetFields()
      this.$emit('update:showDialog', false)
    }
  }

}
</script>

<style>
</style>
